//
//  NearByViewController.m
//  ieGou
//
//  Created by 潮汐 on 15-1-28.
//  Copyright (c) 2015年 chaox. All rights reserved.
//

#import "NearByViewController.h"
#import "NearCell.h"
#import "NDetailsViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface NearByViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate, CLLocationManagerDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *currLocation;
@property (nonatomic, retain) CLPlacemark *placemark;

@end

@implementation NearByViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:95/255.0 green:207/255.0 blue:173/255.0 alpha:1];
    
    [self createSearchBar];
    [self changeButton];
    [self createTableView];
//    [self startLocation];
    
}
//设置UISearchBar
- (void)createSearchBar
{
    //搜索栏设置
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0.0, 0.0, 160, 30)];
    searchBar.placeholder = @"输入商家/分类";
    searchBar.tintColor = [UIColor blackColor];
    searchBar.delegate = self;
    searchBar.keyboardAppearance = UIKeyboardAppearanceDefault;
    searchBar.barStyle = UIBarStyleDefault;
    searchBar.showsCancelButton = YES;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    // 通过遍历 修改cancel样式
    for(id cc in [searchBar subviews])
    {
        for (id zz in [cc subviews]) {
            if([zz isKindOfClass:[UIButton class]])
            {
                UIButton *btn = (UIButton *)zz;
                [btn setTitle:@"搜索"  forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }
        }
    }
    self.navigationItem.titleView = searchBar;
    [searchBar release];
  
}
//searchBar协议方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    // 点击按钮 触发协议button方法
    // 随便写个回收键盘方法
    //可以跳到要到得那一页
    NSLog(@"点击");
    [searchBar resignFirstResponder];
}

- (void)startLocation
{
    self.locationManager = [[CLLocationManager alloc]init];
    // 设置距离筛选器
    self.locationManager.distanceFilter = 10;
    // 设置定位精度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    self.locationManager.delegate = self;
    // 开始实时定位
    [self.locationManager startUpdatingLocation];
    // 始终允许访问
    [_locationManager requestAlwaysAuthorization];
    [_locationManager release];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    self.currLocation = [locations lastObject];
    NSLog(@"经度 = %f 纬度 = %f", self.currLocation.coordinate.latitude, self.currLocation.coordinate.longitude);
    // 位置反编码
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:self.currLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error||placemarks.count==0) {
            NSLog(@"你的地址没找到");
            NSLog(@"%@", error);
        }else {
            self.placemark=[placemarks firstObject];
//            NSLog(@"位置 %@", self.placemark.name); // 位置
        }
    }];
    [self.locationManager stopUpdatingLocation];
    [geocoder release];
    
}
//设置UITableView
- (void)createTableView
{
    //tabBar 不透明
    self.tabBarController.tabBar.translucent = NO;
    //tableView设置
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 99, self.view.frame.size.width,self.view.frame.size.height - 140) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
}

//tableView的方法
//分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}
//判断cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"str";
    NearCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[NearCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"str"];
    }
    cell.aLabel.text = @"nihao";
    return cell;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
//UIView (三个按钮)
- (void)changeButton
{
    //UIView设置
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, 375, 35)];
    view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view];
    self.quanButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, self.view.frame.size.width / 3 , 25)];
    _quanButton.showsTouchWhenHighlighted = YES;
    [_quanButton setTitle:@"全部分类" forState:UIControlStateNormal];
    [_quanButton addTarget:self action:@selector(aButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_quanButton];
    self.quanImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 7, 25, 20)];
    _quanImage.image = [UIImage imageNamed:@"Near5.png"];
    [view addSubview:_quanImage];
    
    self.shiButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 + 5, 5, self.view.frame.size.width  / 3, 25)];
    _shiButton.showsTouchWhenHighlighted = YES;
    [_shiButton setTitle:@"10千米" forState:UIControlStateNormal];
    [_shiButton addTarget:self action:@selector(bButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_shiButton];
    self.shiImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 + 10, 5, 25, 25)];
    _shiImage.image = [UIImage imageNamed:@"Near6.png"];
    [view addSubview:_shiImage];
    
    self.zhiButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 2 / 3 + 10, 5, self.view.frame.size.width / 3, 25)];
    _zhiButton.showsTouchWhenHighlighted = YES;
    [_zhiButton setTitle:@"智能排序" forState:UIControlStateNormal];
    [_zhiButton addTarget:self action:@selector(cButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_zhiButton];
    self.zhiImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * 2 / 3 + 5, 5, 25, 25)];
    _zhiImage.image = [UIImage imageNamed:@"Near7.png"];
    [view addSubview:_zhiImage];
    [view release];
 
}
//全部分类按钮
- (void)aButtonAction:(UIButton *)button
{
    
}
//10千米按钮
- (void)bButtonAction:(UIButton *)button
{
    
}
//智能排序按钮
- (void)cButtonAction:(UIButton *)button
{
    
}
//点击推出下一页(Details页面)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NDetailsViewController *detaVC = [[NDetailsViewController alloc]init];
    [self.navigationController pushViewController:detaVC animated:YES];
    [detaVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
